<?php
  header("Content-Type: text/html;charset=utf-8");
  include_once('./system/dbConn.php');
  
  //连接数据库		
  connect();

		$username = trim($_POST['username']);
		$password = $_POST['password'];
		$gender = $_POST['gender'];
		$birthdate = $_POST['birthdate'];
		$hobby = implode($_POST['hobby'], ',');
		$degree = $_POST['degree'];
		$intro = $_POST['intro'];
    //var_dump($_POST);
  
    if($_FILES["pic"]["error"]>0)
    {
      switch($_FILES["pic"]["error"]){
        case 1:echo"文件尺寸超过了配置文件的最大值"; exit;
        case 2:echo"部分文件上传";  exit;
        case 4:
             echo"没有选择头像文件!";
             //编写SQL语句
             $sql = "insert into users 
             values(null,'$username',md5('$password'),'$gender','$birthdate','$hobby','$degree','$intro',null)";
             break;
             default:echo"未知错误";exit;
      }
    }else{      //上传文件    

       //获取文件拓展名
      $suffix=strrchr($_FILES["pic"]["name"],',');
      //echo $suffix
      
      //指定在服务器上的文件存放路径和文件名
      $filepath="./images/";
      $newname=date("Y mdHis").rand(100,999).$suffix;
      //echo $filepath.$newname.'<br/>';
      //上传文件，如果上传成功，则将用户信息添加到数据库，否则提示“上传失败”
      if(!move_uploaded_file($_FILES["pic"]["tmp_name"],$filepath.$newname)){
        die('图片上传失败');
      }else{
        //编写SQL语句
        $sql = "insert into users 
        values(null,'$username',md5('$passward'),'$gender','$birthdate','$hobby','$degree','$intro','$newname')";
      }
    }
	//echo $sql.'<br/>',
  //执行SQL语句
    $result = mysql_query($sql) or die('插入失败！'.mysql_error());
    //判断是否插入成功
    $num=mysql_affected_rows();

  if($num!=1){
  	echo "注册失败！<br/>";
  	echo "<a href='userReg.html'>返回</a>";	  
  }else{	  
 	echo "注册成功！";
 	echo "请使用<a href='./admin/login.html'>管理员登录</a>查看用户信息，";
	echo "或返回<a href='userReg.html'>注册</a>页面！";	  
  }    
  
?>